﻿/*
#**************************************************************************
#  openSIS is a student information system for public and non-public 
#  schools from Open Solutions for Education, Inc. web: www.os4ed.com
#
#  openSIS is  web-based, open source, and comes packed with features that 
#  include student demographic info, scheduling, grade book, attendance, 
#  report cards, eligibility, transcripts, parent portal, 
#  student portal and more.   
#
#  Visit the openSIS web site at http://www.opensis.com to learn more.
#  If you have question regarding this system or the license, please send 
#  an email to info@os4ed.com.
#
#  This program is released under the terms of the GNU General Public License  
#  as  published by the Free Software Foundation, version 2 of the License. 
#  See license.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#**************************************************************************** 

*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OpenSIS.OpenSISWCFServiceReference;
using System.Data;
using System.ServiceModel;

namespace OpenSIS
{
    public partial class WUCMainMenu : System.Web.UI.UserControl
    {
        static string AddTableList = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                PupulateMenu();
                if (Session["MainMenuSelected"] != null)
                {
                    for (int i = 0; i < this.NavigationMenu.Items.Count; i++)
                    {
                        if (this.NavigationMenu.Items[i].Text == Session["MainMenuSelected"].ToString())
                        {
                            this.NavigationMenu.Items[i].Selected = true;
                            NavigationMenu.DynamicSelectedStyle.CssClass = "cdnavheader_MainMenuSelected";
                            NavigationMenu.StaticSelectedStyle.CssClass = "cdnavheader_MainMenuSelected";
                        }
                    }
                }

                if (Session["SubMenuSelected"] != null)
                {
                    for (int i = 0; i < this.NavigationSubMenu.Items.Count; i++)
                    {
                        if (this.NavigationSubMenu.Items[i].Text == Session["SubMenuSelected"].ToString())
                        {
                            //this.NavigationSubMenu.Items[i].Selected = true;
                            NavigationSubMenu.DynamicSelectedStyle.CssClass = "smheader_Selected";
                            NavigationSubMenu.StaticSelectedStyle.CssClass = "smheader_Selected";
                            int childcount = this.NavigationSubMenu.Items[i].ChildItems.Count;
                            if (childcount == 0)
                            {
                                this.NavigationSubMenu.Items[i].Selected = true;

                            }
                            else
                            {
                                NavigationSubMenu.Items[i].Selectable = true;
                                NavigationSubMenu.Items[i].Selected = true;
                                NavigationSubMenu.Items[i].NavigateUrl = Request.Url.AbsolutePath.ToLower();
                            }
                        }
                    }
                }

            }
        }

        private void PupulateMenu()
        {

            string lang = Session["MLCulture"].ToString();

             ServicePropertiesClient SPClient = new ServicePropertiesClient();
             
             if (lang == "en-US")
             {
                 int menuCount = SPClient.SearchMainMenuItems().Rows.Count;

                 for (int i = 0; i < menuCount; i++)
                 {
                     DataTable dtMM = SPClient.SearchMainMenuItems();
                     MenuItem mainMenuItem = new MenuItem(Convert.ToString(dtMM.Rows[i][1]));
                     mainMenuItem.NavigateUrl = Convert.ToString(dtMM.Rows[i][2]);
                     mainMenuItem.Selectable = true;
                     mainMenuItem.NavigateUrl = "";



                     NavigationMenu.StaticMenuStyle.CssClass = "cdnavheader_menu";
                     NavigationMenu.StaticMenuItemStyle.CssClass = "cdnavheader_menu_links";

                     //if (i < menuCount - 1)
                     //{
                     //    mainMenuItem.SeparatorImageUrl = "~/App_Themes/main/images/separator.png";
                     //}
                     NavigationMenu.Items.Add(mainMenuItem);

                 }
                 if (SPClient.State == CommunicationState.Opened)
                 {
                     SPClient.Close();
                 }
             }
             else
             {

                 // SPClient.MenuType = "MM";
                 //SPClient.LanguageType = lang;

                 int menuCount = SPClient.SearchMainMenuItemsMultilang("MM", lang).Rows.Count;
                 for (int i = 0; i < menuCount; i++)
                 {
                     DataTable dtMM = SPClient.SearchMainMenuItemsMultilang("MM", lang);
                     MenuItem mainMenuItem = new MenuItem(Convert.ToString(dtMM.Rows[i][1]));
                     mainMenuItem.NavigateUrl = Convert.ToString(dtMM.Rows[i][2]);
                     mainMenuItem.Selectable = true;
                     mainMenuItem.NavigateUrl = "";


                     NavigationMenu.StaticMenuStyle.CssClass = "cdnavheader_menu";
                     NavigationMenu.StaticMenuItemStyle.CssClass = "cdnavheader_menu_links";



                     //if (i < menuCount - 1)
                     //{
                     //    mainMenuItem.SeparatorImageUrl = "~/App_Themes/main/images/separator.png";
                     //}


                     NavigationMenu.Items.Add(mainMenuItem);
                     
                 }

                 if (SPClient.State == CommunicationState.Opened)
                 {
                     SPClient.Close();
                 }
             }


            if (Session["MMenuHeader"] != null)
            {
                SubMenuPopulation();
            }
        }

        protected void NavigationMenu_MenuItemClick(object sender, MenuEventArgs e)
        {

            //***********03/052010**********
            ClearSession();

            Session["MainMenuSelected"] = NavigationMenu.SelectedItem.Text;

            Session["SubMenuSelected"] = null;

            if (NavigationMenu.SelectedItem.Text == "School Setup")
            {
                Session["SubMenuSelected"] = "School Information";
            }
            if (NavigationMenu.SelectedItem.Text == "Escuela de configuración")//spanish
            {
                Session["SubMenuSelected"] = "School Information";
            }
            if (NavigationMenu.SelectedItem.Text == "Scuola di installazione")//italian
            {
                Session["SubMenuSelected"] = "Informazione della Scuola";
            }
            if (NavigationMenu.SelectedItem.Text == "Schule Setup")//german
            {
                Session["SubMenuSelected"] = "Schule Auskunft";
            }
            if (NavigationMenu.SelectedItem.Text == "École d'installation")//french
            {
                Session["SubMenuSelected"] = "Renseignements sur l'école";
            }
            if (NavigationMenu.SelectedItem.Text == "Школа Настройка")//russian
            {
                Session["SubMenuSelected"] = "Школьная Информация";
            }
            if (NavigationMenu.SelectedItem.Text == "学校のセットアップ")//japanese
            {
                Session["SubMenuSelected"] = "学校情報";
            }
            if (NavigationMenu.SelectedItem.Text == "학교 설치")//korean
            {
                Session["SubMenuSelected"] = "학교 정보";
            }
            if (NavigationMenu.SelectedItem.Text == "مدرسة الإعداد")//arabic
            {
                Session["SubMenuSelected"] = "المعلومات المدرسية";
            }
            if (NavigationMenu.SelectedItem.Text == "安装学校")//chinese
            {
                Session["SubMenuSelected"] = "学校资料";
            }
            if (NavigationMenu.SelectedItem.Text == "Escola Instalação")//portuguese
            {
                Session["SubMenuSelected"] = "Informação sobre a escola";
            }

            string lang = Session["MLCulture"].ToString();
            SubMenuPopulation();            
            ServicePropertiesClient SPClient = new ServicePropertiesClient();
            Session["CalenderID"] = null;
            //SPClient.MMenuHeader = NavigationMenu.SelectedItem.Text.ToString();
            String  MMenuHeader = NavigationMenu.SelectedItem.Text.ToString();
            Session["MMenuHeader"] = NavigationMenu.SelectedItem.Text;

            if (lang == "en-US")
            {
                DataTable dt = SPClient.MainMenuItemsUrl(MMenuHeader);
                string url = "~//" + dt.Rows[0][0].ToString().Replace("\\", "//");
                SPClient.Close();
                Response.Redirect(url);
                
                //string url = dt.Rows[0][0].ToString();
                //int count = url.Split("\\".ToCharArray()).Length;
                //string nurl = "";
                //for (int j = 0; j < count; j++)
                //{
                //    //nurl = url.Split("\\".ToCharArray())[j].ToString();
                //    nurl = url;
                //    if (nurl.Contains(".aspx"))
                //    {
                //        nurl = nurl.Split(".".ToCharArray())[0].ToString();
                //        nurl = nurl.Replace("\\", "/");
                //        break;
                //    }
                //}
                //Response.Redirect("~//"+nurl);

            }
            else
            {

                DataTable dt = SPClient.MainMenuItemsUrlMultilang(MMenuHeader);
                string url = "~//" + dt.Rows[0][0].ToString().Replace("\\", "//");
                if (SPClient.State == CommunicationState.Opened)
                {
                    SPClient.Close();
                }
                Response.Redirect(url);

                //string url = dt.Rows[0][0].ToString();
                //int count = url.Split("\\".ToCharArray()).Length;
                //string nurl = "";
                //for (int j = 0; j < count; j++)
                //{
                //    //nurl = url.Split("\\".ToCharArray())[j].ToString();
                //    nurl = url;
                //    if (nurl.Contains(".aspx"))
                //    {
                //        nurl = nurl.Split(".".ToCharArray())[0].ToString();
                //        nurl = nurl.Replace("\\","/");
                //        break;
                //    }
                //}
                //Response.Redirect("~//" + nurl);
            }
        }

        public void SubMenuPopulation()
        {
            string lang = Session["MLCulture"].ToString();
            NavigationSubMenu.Items.Clear();

            ServicePropertiesClient SPClient = new ServicePropertiesClient();           

            if (Session["MMenuHeader"] == null)
            {
                Session["MMenuHeader"] = NavigationMenu.SelectedItem.Text;
            }

           // SPClient.MMenuHeader = Session["MMenuHeader"].ToString();
            string MMenuHeader = Session["MMenuHeader"].ToString();

            if (lang == "en-US")
            {
                int subMenucount = SPClient.SearchMainMenuSubMenuItems(MMenuHeader).Rows.Count;
                for (int j = 0; j < subMenucount; j++)
                {
                    if (Convert.ToString(SPClient.SearchMainMenuSubMenuItems(MMenuHeader).Rows[j][4]) == "V")
                    {
                        MenuItem SubMenuItem = new MenuItem(Convert.ToString(SPClient.SearchMainMenuSubMenuItems(MMenuHeader).Rows[j][2]));
                        if (j < subMenucount - 1)
                        {
                            SubMenuItem.SeparatorImageUrl = "~/App_Themes/main/images/separator1.png";
                        }
                        NavigationSubMenu.Items.Add(SubMenuItem);
                        SubMenuItem.NavigateUrl = "";

                      //  SPClient.MSMenuHeader = SubMenuItem.Text.ToString();
                        string MSMenuHeader = SubMenuItem.Text.ToString();
                        SubMenuItem.Selectable = true;

                        
                        int ChildMenucount = SPClient.SearchMainMenuSubMenuChildItems(MMenuHeader,MSMenuHeader).Rows.Count;
                        if (ChildMenucount > 0)
                        {
                            for (int k = 0; k < ChildMenucount; k++)
                            {
                                if (Convert.ToString(SPClient.SearchMainMenuSubMenuChildItems(MMenuHeader,MSMenuHeader).Rows[k]["visible"]) == "V")
                                {
                                    MenuItem ChildMenuItem = new MenuItem(Convert.ToString(SPClient.SearchMainMenuSubMenuChildItems(MMenuHeader, MSMenuHeader).Rows[k]["Main_SubMenu_Child_Header"]));

                                    SubMenuItem.NavigateUrl = Convert.ToString(SPClient.SearchMainMenuSubMenuChildItems(MMenuHeader, MSMenuHeader).Rows[k]["url"]);
                                    SubMenuItem.Selectable = false;

                                    SubMenuItem.ChildItems.Add(ChildMenuItem);
                                }
                            }
                        }

                    }
                }
                if (SPClient.State == CommunicationState.Opened)
                {
                    SPClient.Close();
                }
            }
            else
            {
                int subMenucount = SPClient.SearchSubMenuItemsMultilingual(MMenuHeader).Rows.Count;
                DataTable dtsubmenu = SPClient.SearchSubMenuItemsMultilingual(MMenuHeader);
                for (int j = 0; j < subMenucount; j++)
                {
                    if (Convert.ToString(SPClient.SearchSubMenuItemsMultilingual(MMenuHeader).Rows[j][4]).Trim() == "V")
                    {
                        MenuItem SubMenuItem = new MenuItem(Convert.ToString(SPClient.SearchSubMenuItemsMultilingual(MMenuHeader).Rows[j][2]));
                        if (j < subMenucount - 1)
                        {
                            SubMenuItem.SeparatorImageUrl = "~/App_Themes/main/images/separator1.png";
                        }
                        NavigationSubMenu.Items.Add(SubMenuItem);
                        SubMenuItem.NavigateUrl = "";

                       // SPClient.MSMenuHeader = SubMenuItem.Text.ToString();
                        string MSMenuHeader = SubMenuItem.Text.ToString();

                        SubMenuItem.Selectable = true;


                        int ChildMenucount = SPClient.SearchSubMenuChildItemsMultilingual(MMenuHeader,MSMenuHeader).Rows.Count;
                        if (ChildMenucount > 0)
                        {
                            for (int k = 0; k < ChildMenucount; k++)
                            {
                                if (Convert.ToString(SPClient.SearchSubMenuChildItemsMultilingual(MMenuHeader, MSMenuHeader).Rows[k]["Visible"]).Trim() == "V")
                                {
                                    MenuItem ChildMenuItem = new MenuItem(Convert.ToString(SPClient.SearchSubMenuChildItemsMultilingual(MMenuHeader, MSMenuHeader).Rows[k]["SubMenu_Child_Header"]));

                                    SubMenuItem.NavigateUrl = Convert.ToString(SPClient.SearchSubMenuChildItemsMultilingual(MMenuHeader, MSMenuHeader).Rows[k]["URL"]);
                                    SubMenuItem.Selectable = false;

                                    SubMenuItem.ChildItems.Add(ChildMenuItem);
                                }
                            }
                        }

                    }
                }
                if (SPClient.State == CommunicationState.Opened)
                {
                    SPClient.Close();
                }
            }

            if (NavigationMenu.SelectedItem != null)
            {
                if (Session["MMenuHeader"].ToString() != NavigationMenu.SelectedItem.Text.ToString())
                {
                    Session["MMenuHeader"] = null;
                    SubMenuPopulation();
                }

            }
        }

        protected void NavigationSubMenu_MenuItemClick(object sender, MenuEventArgs e)
        {
            //Session["SubMenuSelected"] = NavigationSubMenu.SelectedItem.Text;
            string lang = Session["MLCulture"].ToString();
            ServicePropertiesClient SPClient = new ServicePropertiesClient();
            Session["CalenderID"] = null;
            //***********03/052010**********
            ClearSession();
            //******************
            string   MMenuHeader = NavigationSubMenu.SelectedItem.Text.ToString();
            if (lang == "en-US")
            {
                DataTable dt = SPClient.SubMenuItemsUrl(MMenuHeader);
                string MSChildMenuHeader = e.Item.Value;
                DataTable dtChild = SPClient.SubMenuChildItemsUrl(MSChildMenuHeader);
                if (dt.Rows.Count > 0)
                {
                    Session["SubMenuSelected"] = NavigationSubMenu.SelectedItem.Text;
                    string url = "~//" + dt.Rows[0][0].ToString().Replace("\\", "//");
                    SPClient.Close();
                    Response.Redirect(url);

                    //string url = dt.Rows[0][0].ToString();                    
                    // int count = url.Split("\\".ToCharArray()).Length;
                    // string nurl = "";
                    // for (int j = 0; j < count; j++)
                    // {
                    //    // nurl = url.Split("\\".ToCharArray())[j].ToString();
                    //     nurl = url;
                    //     if (nurl.Contains(".aspx"))
                    //     {
                    //         nurl = nurl.Split(".".ToCharArray())[0].ToString();
                    //         nurl = nurl.Replace("\\", "/");
                    //         break;
                    //     }
                    // }
                    // Response.Redirect("~//" + nurl);

                }

                if (dtChild.Rows.Count > 0)
                {
                    Session["SubMenuSelected"] = dtChild.Rows[0][1].ToString();
                    string urlChild = "~//" + dtChild.Rows[0][0].ToString().Replace("\\", "//");
                    SPClient.Close();
                    Response.Redirect(urlChild);

                    //string url = dtChild.Rows[0][0].ToString();
                    //int count = url.Split("\\".ToCharArray()).Length;
                    //string nurl = "";
                    //for (int j = 0; j < count; j++)
                    //{
                    //    nurl = url.Split("\\".ToCharArray())[j].ToString();
                    //    nurl = url;
                    //    if (nurl.Contains(".aspx"))
                    //    {
                    //        nurl = nurl.Split(".".ToCharArray())[0].ToString();
                    //        nurl = nurl.Replace("\\", "/");
                    //        break;
                    //    }
                    //}                    
                    //Response.Redirect("~//" + nurl);
                }
            }
            else
            {
                DataTable dt = SPClient.SubMenuItemsUrlMultilingual(MMenuHeader);
                string   MSChildMenuHeader = e.Item.Value;
                DataTable dtChild = SPClient.SubMenuChildItemsUrlMultilingual(MSChildMenuHeader);

                if (dt.Rows.Count > 0)
                {
                    Session["SubMenuSelected"] = NavigationSubMenu.SelectedItem.Text;
                    string url = "~//" + dt.Rows[0][0].ToString().Replace("\\", "//");
                    if (SPClient.State == CommunicationState.Opened)
                    {
                        SPClient.Close();
                    }
                    Response.Redirect(url);


                    //string url = dt.Rows[0][0].ToString();
                    //int count = url.Split("\\".ToCharArray()).Length;
                    //string nurl = "";
                    //for (int j = 0; j < count; j++)
                    //{
                    //    //nurl = url.Split("\\".ToCharArray())[j].ToString();
                    //    nurl = url;
                    //    if (nurl.Contains(".aspx"))
                    //    {
                    //        nurl = nurl.Split(".".ToCharArray())[0].ToString();
                    //        nurl = nurl.Replace("\\", "/");
                    //        break;
                    //    }
                    //}
                    //Response.Redirect("~//" + nurl);
                }

                if (dtChild.Rows.Count > 0)
                {
                    Session["SubMenuSelected"] = dtChild.Rows[0][1].ToString();
                    string urlChild = "~//" + dtChild.Rows[0][0].ToString().Replace("\\", "//");
                    if (SPClient.State == CommunicationState.Opened)
                    {
                        SPClient.Close();
                    }
                    Response.Redirect(urlChild);


                    //string url = dtChild.Rows[0][0].ToString();
                    //int count = url.Split("\\".ToCharArray()).Length;
                    //string nurl = "";
                    //for (int j = 0; j < count; j++)
                    //{
                    //    //nurl = url.Split("\\".ToCharArray())[j].ToString();
                    //    nurl = url;
                    //    if (nurl.Contains(".aspx"))
                    //    {
                    //        nurl = nurl.Split(".".ToCharArray())[0].ToString();
                    //        nurl = nurl.Replace("\\", "/");
                    //        break;
                    //    }
                    //}
                    //Response.Redirect("~//" + nurl);
                }
            }
        }


        //***********03/05/2010**************
        protected void ClearSession()
        {


            OpenSISWCFServiceStudentEditClient EditFieldValue = new OpenSISWCFServiceStudentEditClient();

            int SchoolID = Convert.ToInt32(Session["SchoolID"]);
            DataTable dt = EditFieldValue.GetSchoolSpecificCategoryStudentEdit(SchoolID);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Session[dt.Rows[i]["Cat_Id"].ToString()] = null;
            }

            string[] SessionTable = AddTableList.Split(';');
            int SessionTableId = 1;
            for (int i = 1; i < SessionTable.Length; i++)
            {
                string sessionTab = "Address" + SessionTableId.ToString();
                Session[sessionTab] = null;
            }
            Session["SchoolInfo"] = null;
            Session["Enroltable"] = null;
            Session["MedicalInfo"] = null;
            Session["ImmuTable"] = null;
            Session["AlertTable"] = null;
            Session["NurseVisitTable"] = null;
            Session["VaccinationForInsert"] = null;
            Session["VaccinationForGrid"] = null;
            Session["SpecialGiftForInsert"] = null;
            Session["SpecialEduForInsert"] = null;

        }
    }
}